package com.thb.pay.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.thb.pay.core.utils.PageResult;
import com.thb.pay.db.service.impl.BaseServiceImpl;
import com.thb.pay.db.utils.PageResultUtils;
import com.thb.pay.db.utils.PageUtils;
import com.thb.pay.db.utils.QueryUtils;
import com.thb.pay.entity.SysConfig;
import com.thb.pay.mapper.SysConfigMapper;
import com.thb.pay.service.SysConfigService;
import com.thb.pay.vo.req.SysConfigPageReq;
import com.thb.pay.vo.req.SysConfigReq;
import com.thb.pay.vo.resp.SysConfigVo;
import com.xiaoleilu.hutool.collection.CollUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

/**
 * <p>
 * 系统-配置表 服务实现类
 * </p>
 *
 * @author lucky
 * @since 2019-07-21
 */
@Service
public class SysConfigServiceImpl extends BaseServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {
    @Override
    public PageResult<SysConfigVo> findPage(SysConfigPageReq pageReq) {
        IPage<SysConfig> currentPage = PageUtils.buildPage(pageReq);
        QueryWrapper wrapper = QueryUtils.buildQueryPage(pageReq);
        IPage<SysConfig> pageResult = super.page(currentPage, wrapper);
        return PageResultUtils.build(pageResult, SysConfigVo.class);
    }

    @Override
    public List<SysConfigVo> findListByType(String type) {
        Assert.notNull(type, "config.type.not.null");
        List<SysConfigVo> targetList = Lists.newArrayList();
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getType, type);
        List<SysConfig> resList = super.selectList(wrapper);
        resList.forEach(sysConfig -> {
            SysConfigVo targetResult = new SysConfigVo();
            BeanUtils.copyProperties(sysConfig, targetResult);
            targetList.add(targetResult);
        });
        return targetList;
    }

    @Override
    public SysConfigVo getByCode(String code) {
        Assert.notNull(code, "config.code.not.null");
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getCode, code);
        List<SysConfig> resList = super.selectList(wrapper);
        if (CollUtil.isNotEmpty(resList)) {
            SysConfigVo targetResult = new SysConfigVo();
            BeanUtils.copyProperties(resList.get(0), targetResult);
            return targetResult;
        }
        return null;
    }

    @Override
    public void updateAll(List<SysConfigReq> configReqList) {
        Assert.notEmpty(configReqList, "config.info.list.not.null");
        configReqList.forEach(sysConfigReq -> {
            SysConfig targetResult = new SysConfig();
            BeanUtils.copyProperties(sysConfigReq, targetResult);
            super.updateById(targetResult);
        });
    }

    @Override
    public void save(SysConfigReq configReq) {
        Assert.notNull(configReq, "config.info.not.null");
        SysConfig targetResult = new SysConfig();
        BeanUtils.copyProperties(configReq, targetResult);
        super.save(targetResult);
    }
}
